set -ex
curdir=$(pwd)
build(){
    cd $1
    genpspec > pspec.xml
    if find $curdir/../binary_packages/ | grep -e "/$(basename $1)-[0-9]" ; then
        echo skip $1
    else
        if [[ -f ./.rootbuild ]] ; then
            unshare -ru inary bi --ignore-dep -y
        else
            inary bi --ignore-dep -y
        fi
        mv *.inary $curdir/../build/
    fi
    cd $curdir
}

mkdir -p ../build || true
find | grep pspec.xml | sort -V | xargs dirname | while read line ; do
    build $line
done
